//
//  EOCClassSQL.m
//  EOCSQLClass
//
//  Created by EOC.
//  Copyright ©  EOC. All rights reserved.
//

#import "EOCClassSQL.h"
#import <sqlite3.h>

/*
 
 */

@implementation EOCClassSQL


// 第一步创建db
+ (void)createEOCDBFile{
    
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *eocDBFile = [filePath stringByAppendingPathComponent:@"eoc.db"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:eocDBFile]) {
        
        if([[NSFileManager defaultManager] createFileAtPath:eocDBFile contents:nil attributes:nil]){
            NSLog(@"创建成功");
        }else {
            NSLog(@"创建失败");
        }
        
    }else{
        NSLog(@"文件存在了");
    }
}

static sqlite3 *database;
//
+ (void)createEOCDBFileSQL{
    
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *eocDBFile = [filePath stringByAppendingPathComponent:@"eoc.db"];
    
    // 如果文件不存在，就创建该文件，其配置属性是 SQLITE_OPEN_CREATE
    
    int status =  sqlite3_open_v2([eocDBFile UTF8String], &database, SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE, NULL);
    
    if (status != SQLITE_OK) {
        NSLog(@"sqlite3_open_v2 fail");
    }
}


// 第二步创建表
/*
 sqlite3 *database; 文件指针
 sql 语句是 对文件进行操作
 */

+ (void)createTable{
    
    // 操作   ⚠️sqlite3_stmt 失败一定要释放 即进行sqlite3_finalize操作
    // sqlite3_stmt 默认不能重复在利用
    
    // 1 先语句
    NSString *sqlStr = @"create table if not exists eoctable(id char, name char)";
    
    sqlite3_stmt *stmt; // 可以理解为 代表一条指令
    // 2 检测语句
    int status = sqlite3_prepare_v2(database, [sqlStr UTF8String], -1, &stmt, 0);
    
    if (status != SQLITE_OK) {
        
        NSLog(@"sqlite3_prepare_v2 fail");
        sqlite3_finalize(stmt);
        return;
    }
    // 3 执行指令
    status = sqlite3_step(stmt);
    if (status != SQLITE_DONE) {
        NSLog(@"sqlite3_step fail");
    }
    // 4 结束
    sqlite3_finalize(stmt);
    
}


// 增删改   查
+ (void)insertSQLValue:(NSString*)idStr name:(NSString*)name{
    
    // 操作   ⚠️sqlite3_stmt 失败一定要释放 即进行sqlite3_finalize操作
    // sqlite3_stmt 默认不能重复在利用
    // 1 先语句
    NSString *sqlStr = [NSString stringWithFormat:@"insert into eoctable values('%@', '%@')", idStr, name];
    
    sqlite3_stmt *stmt; // 可以理解为 代表一条指令
    // 2 检测语句
    int status = sqlite3_prepare_v2(database, [sqlStr UTF8String], -1, &stmt, 0);
    
    if (status != SQLITE_OK) {
        
        NSLog(@"sqlite3_prepare_v2 fail");
        sqlite3_finalize(stmt);
        return;
    }
    // 3 执行指令
    status = sqlite3_step(stmt);
    if (status != SQLITE_DONE) {
        NSLog(@"sqlite3_step fail");
    }
    // 4 结束
    sqlite3_finalize(stmt);
    
}


+ (NSArray*)getAllRecord{
    
    // 1 先语句
    NSString *sqlStr = @"select *from eoctable";
    
    sqlite3_stmt *stmt; // 可以理解为 代表一条指令
    // 2 检测语句
    int status = sqlite3_prepare_v2(database, [sqlStr UTF8String], -1, &stmt, 0);
    
    if (status != SQLITE_OK) {
        
        NSLog(@"sqlite3_prepare_v2 fail");
        sqlite3_finalize(stmt);
        return nil;
    }
    // 3 执行指令
    NSMutableArray *recordAry = [NSMutableArray new];
    while(sqlite3_step(stmt) == SQLITE_ROW){
        
        
        
        int columns = sqlite3_column_count(stmt);
        
        NSMutableDictionary *infoDict = [NSMutableDictionary new];
        for (int i = 0; i < columns; i++) {
        
            char *keyC = (char *)sqlite3_column_name(stmt, i);
            NSString *keyStr = [NSString stringWithUTF8String:keyC];
            
            char *valueC = (char *)sqlite3_column_text(stmt, i);
            NSString *valueStr = [NSString stringWithUTF8String:valueC];
            
            [infoDict setObject:valueStr forKey:keyStr];
            
        }
        [recordAry addObject:infoDict];
        
        
//        char *idC   =  (char*)sqlite3_column_text(stmt, 0);
//        char *nameC = (char*)sqlite3_column_text(stmt, 1);
//
//        NSString *idStr = [NSString stringWithUTF8String:idC];
//        NSString *nameStr = [NSString stringWithUTF8String:nameC];
//        NSMutableDictionary *infoDict = [NSMutableDictionary new];
//        [infoDict setObject:idStr forKey:@"id"];
//        [infoDict setObject:nameStr forKey:@"name"];
//        [recordAry addObject:infoDict];
    }

    // 4 结束
    sqlite3_finalize(stmt);
    
    return recordAry;
}

// 第三步操作表


// 第四步 关闭

+ (void)closeSQL{
    
    sqlite3_close_v2(database);
    
}



@end
